﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using pheidippides.logging.Classes;

namespace pheidippides.app.Forms
{
    public partial class LoggingForm : Form
    {
        public LoggingForm()
        {
            InitializeComponent();

            List<LogEntry> Listy = Logger.GetInstance().GetLogEntrys();

            foreach (LogEntry item in Listy)
            {
                WriteLogEntry(item);
            }

            Logger.GetInstance().OnNewLogEntry += Logger_OnNewLogEntry;
        }

        private void Logger_OnNewLogEntry(LogEntry LogEntry)
        {
            WriteLogEntry(LogEntry);
        }

        private void WriteLogEntry(LogEntry Entry)
        {
            textBoxLogTrace.AppendText(Entry.TimeStamp.ToLongTimeString() + "." +
                                       Entry.TimeStamp.Millisecond.ToString("000") + "\t" + Entry.LogLevel + "\t" +
                                       Entry.Message + Environment.NewLine);
        }

        private void LoggingForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            Logger.GetInstance().OnNewLogEntry -= Logger_OnNewLogEntry;
            FormManager.FormManager.GetInstance().DisposeForm(this);
        }
    }
}